﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace UberChess.UserInput
{
    public enum KeyType
    {
        Left,
        Right,
        F11,
        Space,
        NoInput
    } ;

    public static class Input
    {
        public static IMousePosition HandleMouseInput(MouseState mouse)
        {
            if(MouseIsWithinBounds(mouse))
            {
                if(mouse.LeftButton == ButtonState.Pressed)
                {
                    return new MouseClickPosition(new Vector2((mouse.X-Constants.HORIZONTALOFFSET)/Constants.PIECEWIDTH, (mouse.Y-Constants.VERTICALOFFSET)/Constants.PIECEHEIGHT));
                }

                return new MouseHoverPosition(new Vector2((mouse.X - Constants.HORIZONTALOFFSET) / Constants.PIECEWIDTH, (mouse.Y - Constants.VERTICALOFFSET) / Constants.PIECEHEIGHT));
            }

            if (MouseHoversUndoButton(mouse))
            {
                if(mouse.LeftButton == ButtonState.Pressed)
                {
                    return new MouseUndoClick();
                }
                return new MouseUndoHover();
            }
            return new MouseHoverPosition(new Vector2(-1, -1));
        }

        private static bool MouseHoversUndoButton(MouseState mouse)
        {
            return new Rectangle((int)Constants.UNDOBUTTONPOSITION.X, (int)Constants.UNDOBUTTONPOSITION.Y,
                              (int)Constants.UNDOBUTTONWIDTHHEIGHT.X, (int)Constants.UNDOBUTTONWIDTHHEIGHT.Y).Contains(mouse.X, mouse.Y);
        }

        private static bool MouseIsWithinBounds(MouseState mouse)
        {
            return
                new Rectangle(Constants.HORIZONTALOFFSET, Constants.VERTICALOFFSET, Constants.GAMEBOARDPIXELWIDTH,
                              Constants.GAMEBOARDPIXELWIDTH).Contains(mouse.X, mouse.Y);
        }

        public static KeyType HandleKeyboardInput(KeyboardState keys)
        {
            if (keys.IsKeyDown(Keys.Left))
                return KeyType.Left;

            if (keys.IsKeyDown(Keys.Right))
                return KeyType.Right;

            if (keys.IsKeyDown(Keys.F11))
                return KeyType.F11;

            if (keys.IsKeyDown(Keys.Space))
                return KeyType.Space; 

            return KeyType.NoInput;
        }
    }
}
